package com.example.psychologicalappointmentsystem.config.web;

import com.example.psychologicalappointmentsystem.config.auth.ThreadLocalInterceptor;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * web拦截器
 */
@Configuration
public class WebInterceptor implements WebMvcConfigurer {
    @Autowired
    private ThreadLocalInterceptor threadLocalInterceptor;

    // 不做权限拦截的接口
    public static List<String> excludeUrls = Lists.newArrayList();

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        excludeUrls.add("/api/user/login");// 登录接口
        excludeUrls.add("/api/user/register");// 登录接口
        excludeUrls.add("/ws/chat");
        excludeUrls.add("/send-message/**");


        /**
         * 登录拦截器
         */
        registry.addInterceptor(threadLocalInterceptor)
                .addPathPatterns("/api/**")
                .excludePathPatterns(excludeUrls);// 要排除拦截的url
    }
    /**
     * 由于Knife4j和springboot高版本冲突，这里需要手动加上配置
     * 需要结合启动类的@EnableWebMvc使用
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // Knife4j 访问地址：http://host:port/doc.html
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
